var TheGame = require('geoTheGame');
var ScoreEffect = require('geoScoreEffectGroupController');
var MoveTo = require('geoTweenMoveTo');
var ArmatureController = require('geoArmatureController');
var ReadyCounter = require('geoReadyCounterController');
var Rotater = require('geoRotater');
var ValueObject = require('geoValueObject');
var Utils = require('geoUtils');

cc.Class({
    extends: TheGame,
    // extends: cc.Component,

    properties: {
        scoreEffects: {
            default: [],
            type: [ScoreEffect],
        },
        audioDemo: {
            default: null,
            type: cc.AudioClip,
        },
        audioButton: {
            default: null,
            type: cc.Button,
        },
        moveTo: {
            default: null,
            type: MoveTo,
        },
        anim: {
            default: null,
            type: ArmatureController
        },
        ready: {
            default: null,
            type: ReadyCounter,
        },
        rotater: {
            default: null,
            type: Rotater,
        },
        valueObject: {
            default: null,
            type: ValueObject,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
    },
    //<-----------------得分效果器控制-------------------
    addScore() {
        // 如果this._score不存在就让它为0，否则就用它现在的值
        this._score = (void 0 === this._score) ? 0 : this._score;
        // 一次加
        this._score++;
        //
        this._setScore(this._score);
    },
    resetScore() {
        this._score = 0;
        //
        this._setScore(this._score);
    },
    _setScore(score) {
        // 给所有得分效果器设置得分
        this.scoreEffects.forEach((element => {
            element.setScore(score);
        }).bind(this));
        // 顶部栏的分数使用
        if (this.gameMaster.ui.topBar !== null) {
            // 为体现阶段效果与完成效果，先重置为0
            this.gameMaster.ui.topBar.setScore(0);
            //
            this.gameMaster.ui.topBar.setScore(score,
                (() => {   // 所有分数效果完毕，闪5下，1秒
                    this.gameMaster.ui.topBar.node.runAction(cc.blink(1, 5));
                    cc.log('complete');
                }).bind(this),
                (() => {   // 每加完一颗星，闪3下，0.3秒
                    this.gameMaster.ui.topBar.node.runAction(cc.blink(0.3, 3));
                    cc.log('step');
                }).bind(this));
        }
    },
    ////////////////////////////////////////////////////
    //<-----------------场景切换，传递数据-------------------
    passDataToResultScene() {
        // 设置好要传递的信息
        this.data.score = this._score;
        // 调用父类geoGamePlay的方法，去结果场景
        this.gotoResultScene();
    },
    ////////////////////////////////////////////////////
    //<-----------------声音播放-------------------
    ////////////////////////////////////////////////////
    playAudioAndBlink() {
        // 可使用的音频播放方法由: playMusic, playSFX, playVoice,
        // 用法都一样，只是它们分别受不同的音量控制
        this.gameMaster.audio.playMusic(this.gameMaster.audioDemo, false, 1, (() => {
            this.gameMaster.audioButton.node.runAction(cc.blink(2, 3));
        }).bind(this));
    },
    //<-----------------位移操作geoTweenMoveTo,使用预先配置的索引-------------------
    moveToPos0(e, customData) {
        this._moveToPos(0, customData);
    },
    moveToPos1(e, customData) {
        this._moveToPos(1, customData);
    },
    _moveToPos(i, customData) {
        customData = (customData === void 0) ? '' : customData;

        var useMove = true;
        var useScale = (customData.indexOf('scale') != -1);
        var useRotation = (customData.indexOf('rotation') != -1);


        this.moveTo.doTween(i, 2, (() => {
            // 移动完成闪三下
            this.moveTo.target.runAction(cc.blink(0.5, 3));
        }).bind(this), useMove, useScale, useRotation);
    },
    instantToPos3() {
        // 2就是3号位
        this.moveTo.setToIndex(2);
    },
    ////////////////////////////////////////////////////
    //<-----------------位移操作geoTweenMoveTo,使用直接节点引用-------------------
    comeHere(e, customData) {
        customData = (customData === void 0) ? '' : customData;

        var useMove = true;
        var useScale = (customData.indexOf('scale') != -1);
        var useRotation = (customData.indexOf('rotation') != -1);

        var node = e.target;
        this.moveTo.doTweenToNode(node, 1, (() => {
            // 移动完成闪三下
            this.moveTo.target.runAction(cc.blink(0.5, 3));
        }).bind(this), useMove, useScale, useRotation);
    },

    instantHere(e, customData) {
        customData = (customData === void 0) ? '' : customData;

        var useMove = true;
        var useScale = (customData.indexOf('scale') != -1);
        var useRotation = (customData.indexOf('rotation') != -1);

        var node = e.target;
        this.moveTo.setToNode(node, useMove, useScale, useRotation);
    },

    pauseAction() {
        this.moveTo.pauseTween();
    },
    resumeAction() {
        this.moveTo.resumeTween();
    },
    ////////////////////////////////////////////////////

    //<-----------------使用geoArmatureController播放动画-------------------
    animWalk() {
        // 这里的playTimers为-1，实际上不会调用complete回调
        this.anim.fadeInAnim('walk', 0.3, -1, (() => {
            this.anim.node.runAction(cc.blink(0.5, 3));
        }).bind(this));
    },
    animJump() {
        this.anim.fadeInAnim('jump', 0.3, -1, (() => {
            this.anim.node.runAction(cc.blink(0.5, 3));
        }).bind(this));
    },
    animAttack() {
        this.anim.fadeInAnim('attack1_+1', 0.3, 1, (() => {
            this.anim.node.runAction(cc.blink(0.5, 3));
        }).bind(this));
    },
    ////////////////////////////////////////////////////
    //<-----------------使用geoArmatureController播放动画-------------------
    useReadyCounter(e) {
        this.ready.doCount((() => {
            // 预备结束让按钮闪三闪
            e.target.runAction(cc.blink(0.5, 3));
        }).bind(this));
    },
    ////////////////////////////////////////////////////
    //<-----------------使用geoRotate进行旋转-------------------
    toggleRotate() {
        if (this.rotater.isRotating === true)
            this.rotater.stopRotate();
        else
            this.rotater.doRotate();
    },
    ////////////////////////////////////////////////////
    //<-----------------使用geoUtils.array.random从数组中随机选取指定数量的元素-------------------
    randomArray() {
        var arrayFrom = [1, 2, 3, 4, 5, 6, 7, 8, '香蕉', '苹果', '鸭梨'];
        // 可以在控制台看到随机取出的数组，并且从原数组剔除
        var arrayRet = Utils.array.random(arrayFrom, 2, true);
        cc.log("随机取2个:" + arrayRet);
        // 这里不会看到提出的元素
        arrayRet = Utils.array.random(arrayFrom, arrayFrom.length);
        cc.log("乱序原数组:" + arrayRet);
    },
    ////////////////////////////////////////////////////
    //<-----------------使用geoUtils.array.random从数组中随机选取指定数量的元素-------------------
    changeValueObject(e, customData) {
        // 这里演示随意在valueObject上添加需要的数据
        cc.log('last customData on valueObject: ' + this.valueObject.customData);
        this.valueObject.customData = customData;
        cc.log('now customData on valueObject: ' + this.valueObject.customData);
        // 下面演示会触发onValueObjectChanged的数据设置方式
        this.valueObject.setData(customData);
    },
    onValueObjectChanged(e, data) {
        // 将this.valueObject.getData()替换成data也一样
        cc.log('onValueObjectChanged: ' + this.valueObject.getData());
    },
    ////////////////////////////////////////////////////
    //<-----------------使用DragAndDropManager实现拖放操作-------------------
    onDropItem(e, draggee, droppee) {
        draggee.runAction(cc.blink(0.5, 3));
        droppee.runAction(cc.blink(0.5, 3));
    }
    ////////////////////////////////////////////////////

});
